﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FormMove.cs" company="Marcelo Roca">
//   Copyright (c) 2011 Marcelo Roca. All rights reserved.
// </copyright>
// <author name="Marcelo Roca" email="marce@marceloroca.com"/>
// <license>
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see http://www.gnu.org/licenses/.
// </license>
// --------------------------------------------------------------------------------------------------------------------

using UnityEngine;

namespace GameKits.Unity.GuiForms.Forms
{
    public sealed class FormMove : FormCommand
    {
        /// <summary>
        /// La velocidad (pixels por segundo) del movimiento
        /// </summary>
        private readonly float speed;

        /// <summary>
        /// La nueva posición
        /// </summary>
        private readonly Vector2 newPosition;

        /// <summary>
        /// Cuantos segundos va a tardar el movimiento
        /// </summary>
        private float seconds;

        /// <summary>
        /// Cuanto tiempo ha transcurrido
        /// </summary>
        private float elapsedSeconds;

        /// <summary>
        /// Initializes a new instance of the <see cref="FormMove"/> class.
        /// </summary>
        /// <param name="speed">
        /// The seconds.
        /// </param>
        /// <param name="newPosition">
        /// The new position.
        /// </param>
        public FormMove(float speed, Vector2 newPosition)
        {
            this.speed = speed;
            this.newPosition = newPosition;
        }

        /// <summary>
        /// Ejecutamos el comando
        /// </summary>
        /// <returns>
        /// True si ya se terminó de ejecutar
        /// </returns>
        protected override bool DoExecute()
        {
            if (Event.current.type == EventType.Repaint)
            {
                this.elapsedSeconds += Time.deltaTime;
            }

            return true;
        }


        /// <summary>
        /// Inicializamos el movimiento
        /// </summary>
        protected override void Initialize()
        {
            var dX = this.newPosition.x - this.Parent.WindowRect.x;
            var dY = this.newPosition.y - this.Parent.WindowRect.y;

            var aX = Mathf.Abs(dX);
            var aY = Mathf.Abs(dY);

            if (aX > aY)
            {
                this.seconds = aX / this.speed;
            }
            else
            {
                this.seconds = aY / this.speed;
            }
        }
    }
}
